//
//  WHNewFeatureViewController.swift
//  8.31-WH微博
//
//  Created by 吴涵 on 15/9/9.
//  Copyright © 2015年 wuhan. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let WHNum = 4

class WHNewFeatureViewController: UICollectionViewController {
    
    init(){
    super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
prepareLayout()
        
    }

    private func prepareLayout(){
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false

    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return WHNum
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.redColor() : UIColor .greenColor()
    
        cell.imageIndex = indexPath.item
        
        if indexPath.item == WHNum - 1 {
//            老师的用到了
//            
//            // indexPath 参数是之前显示的 cell 的 indexPath
//            override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//                
//                // 取当前显示 cell 的 indexPath
//                let path = collectionView.indexPathsForVisibleItems().last!
//                
//                // 判断是否是最后一个
//                if path.item == HMNewFeatureCount - 1 {
//                    // 获取cell
//                    let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
//                    cell.showStartButton()
//                }
//            }
            // 老师使用了代理的方法实现的
//            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
        cell.showStartBtn()
        }
        return cell
    }
//            老师的用到了
    //
    //            // indexPath 参数是之前显示的 cell 的 indexPath
    //            override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    //
    //                // 取当前显示 cell 的 indexPath
    //                let path = collectionView.indexPathsForVisibleItems().last!
    //
    //                // 判断是否是最后一个
    //                if path.item == HMNewFeatureCount - 1 {
    //                    // 获取cell
    //                    let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
    //                    cell.showStartButton()
    //                }
    //            }

}
private class NewFeatureCell: UICollectionViewCell {
    
    var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            self.startBtn.hidden = true
        }
    }
    @objc func btnClick(){
     NSNotificationCenter.defaultCenter().postNotificationName(WHSwithRootControllerNotification, object: nil)
    }
    
    private func showStartBtn(){
    startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
    
        }

    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
    
    addSubview(iconView)
        addSubview(startBtn)
        
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 100))
        
        iconView.frame = bounds
    }
    
    private lazy var iconView: UIImageView = UIImageView()
    
    private lazy var startBtn: UIButton = {
    let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return btn
    }()
    
}
